package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityXfZoneDate;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfZoneDate;
import com.ocom.payconfig.service.XfZoneDateService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfZoneDateServiceImpl extends ServiceImpl<IDaoXfZoneDate, EntityXfZoneDate> implements XfZoneDateService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
	public PageInfo getConsZoneDateList(Long companyId, Integer page, Integer size) {
		QueryWrapper<EntityXfZoneDate> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", companyId);
		if (page <= 0) {
			PageHelper.startPage(1, 500);
		} else {
			PageHelper.startPage(page, size);
		}
		List<EntityXfZoneDate> pointInfoPageInfo = this.list(queryWrapper);
		PageInfo pageInfo = new PageInfo<>(pointInfoPageInfo);
		return pageInfo;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
	public EntityXfZoneDate setConsDateZone(Long companyId, EntityXfZoneDate entityXfZoneDate) {
		QueryWrapper<EntityXfZoneDate> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", companyId);
		queryWrapper.eq("day_date", entityXfZoneDate.getDayDate());
		queryWrapper.eq("date", entityXfZoneDate.getDate());
		queryWrapper.last("limit 1");
		EntityXfZoneDate entityXfZoneDate1 = this.getOne(queryWrapper);
		if (entityXfZoneDate1 != null) {
			entityXfZoneDate1.setZoneNo(entityXfZoneDate.getZoneNo());
			boolean b = this.updateById(entityXfZoneDate1);
			if (b) {
				return entityXfZoneDate1;
			}
		} else {
			boolean b = this.save(entityXfZoneDate);
			if (b) {
				return entityXfZoneDate;
			}
		}
		return null;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
	public Boolean removeConsDateZone(Long companyId, String date) {
		/**
		 * 业务逻辑：
		 * 1、从分库2.3.34消费时区设置表xf_zone_date删除com_id、day_date=1、date匹配的记录。
		 * 2、修改设备时区版本删除redis中key=( com_id)的消费时区记录。（参照消费操作微服务3.7的业务逻辑）
		 */
		QueryWrapper<EntityXfZoneDate> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", companyId);
		queryWrapper.eq("day_date", 1);
		queryWrapper.eq("date", date);
		return this.remove(queryWrapper);
	}
}
